package com.haha.community.entity;

//用来给页面传入页码相关的信息，有多少也，当前是第几页，封装成该组件
public class Page {
    //默认当前页码为1
    private int currentPage = 1;
    //默认一页显示10条数据
    private int limit = 10;
    //数据总数，用于计算总的页数,从数据库中查询得到，用于确定总页数
    private int count;
    //查询路径（用于复用一些链接，简化页面的逻辑）
    private String path;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if (currentPage >= 1) {
            this.currentPage = currentPage;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit >= 1 && limit <= 100) {
            this.limit = limit;
        }
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        if (count >= 0) {
            this.count = count;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    //根据当前页的得到起始行，便于数据库查询使用
    public int getOffset() {

        return (currentPage - 1) * limit;
    }

    //用来获取总的页数，在页面上显示
    public int getTotal() {
        if (count % limit == 0) {
            return count / limit;
        }
        return count / limit + 1;
    }

    //获取页面显示从第几页到第几页
    public int getFrom() {
        int from = currentPage - 2;
        return from < 1 ? 1 : from;
    }

    //如果超出范围就显示最后的范围
    public int getTo() {
        int to = currentPage + 2;
        int total = getTotal();
        return to > total ? total : to;
    }

    @Override
    public String toString() {
        return "Page{" +
                "currentPage=" + currentPage +
                ", limit=" + limit +
                ", count=" + count +
                ", path='" + path + '\'' +
                '}';
    }
}
